package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.BannerDao;
import com.wanlian.entity.Banner;
import com.wanlian.model.ResultModel;
import com.wanlian.util.DateUtil;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class BannerDaoImpl extends GenericDaoImpl<Banner> implements BannerDao {


    public BannerDaoImpl() {
        super(Banner.class);
    }

    @Override
    public void doedit(Banner banner) {
        banner.setLastUpdateTime(DateUtil.currentTimestamp());
        this.update(banner);
    }

    @Override
    public Banner queryBanner(int id) {
        return this.queryById(id);
    }

    @Override
    public List<Banner> queryBannerList(Page page) {
        return this.queryForListByPage("from " + entityClassName() + " where isenable = 1 order by istop desc, lastupdatetime desc, createtime desc", new Object[]{}, page);
    }


    @Override
    public ResultModel queryBannersList(PageCondition pageCondition) {
        String[] column = {"id", "attribute1", "createtime", "imgpath", "isenable", "istop", "lastupdatetime", "title", "username"};
        String sql = "select b.id,b.attribute1,b.createtime,b.imgpath,b.isenable,b.istop,b.lastupdatetime,b.title,u.username from tb_banner b left join tb_user u on b.userid=u.id";
        StringBuffer sqlWhere = new StringBuffer(" where 1 = 1");
        ArrayList<Object> params = new ArrayList<Object>();
        if (pageCondition.isExistByKey("username")) {
            String username = (String) pageCondition.getConditions().get("username");
            if (StringUtil.hasValue(username)) {
                sqlWhere.append(" and u.username=? ");
                params.add(username);
            }
        }
        if (pageCondition.isExistByKey("isenable")) {
            String isEnable = (String) pageCondition.getConditions().get("isenable");
            if (StringUtil.hasValue(isEnable)) {
                sqlWhere.append(" and b.isenable=? ");
                params.add(Integer.parseInt(isEnable));
            }

        }
        if (pageCondition.isExistByKey("title")) {
            String title = (String) pageCondition.getByKey("title");
            if (StringUtil.hasValue(title)) {
                sqlWhere.append(" and b.title like ?");
                params.add("%" + title + "%");
            }
        }
        if (pageCondition.isExistByKey("startTime")) {
            String startTime = (String) pageCondition.getByKey("startTime");
            if (StringUtil.hasValue(startTime)) {
                sqlWhere.append(" and b.createtime >= ?");
                Timestamp ts = Timestamp.valueOf(startTime + " 00:00:00.00");
                params.add(ts);
            }
        }
        if (pageCondition.isExistByKey("endTime")) {
            String endTime = (String) pageCondition.getByKey("endTime");
            if (StringUtil.hasValue(endTime)) {
                sqlWhere.append(" and b.createtime <= ?");
                Timestamp ts = Timestamp.valueOf(endTime + " 23:59:59.00");
                params.add(ts);
            }
        }
        List<Map<String, Object>> list = this.queryForListObjectByPage(sql + sqlWhere + " order by b.istop desc, b.lastupdatetime desc, b.createtime desc", params.toArray(), pageCondition.getPage(), column);
        int count = this.queryCountByParam("select count(1) from tb_banner b left join tb_user u on b.userid=u.id " + sqlWhere, params.toArray());
        ResultModel resultModel = new ResultModel(true);
        resultModel.setCount(count);
        resultModel.setObject(list);
        return resultModel;
    }

    @Override
    public List<Banner> queryAllForApp() {
        StringBuffer sqlWhere = new StringBuffer(" where isenable = 1 ");
        ArrayList<Object> params = new ArrayList<Object>();
        return this.queryForList("from " + entityClassName() + sqlWhere + " order by istop desc", params.toArray());
    }

    @Override
    public boolean setIsTop(int id) {
        List<Banner> bannerList = this.queryAll();
        for (Banner banner : bannerList) {
            if (id == banner.getId()) {
                banner.setTop(true);
                this.update(banner);
            } else {
                banner.setTop(false);
                this.update(banner);
            }
        }
        return true;
    }

    @Override
    public boolean setEnable(int id) {
        Banner banner = this.queryBanner(id);
        if (banner != null) {
            if (banner.isEnable()) {
                return false;
            } else {
                banner.setEnable(true);
                banner.setLastUpdateTime(banner.getLastUpdateTime());
            }
            this.update(banner);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean setDisable(int id) {
        Banner banner = this.queryBanner(id);
        if (banner != null) {
            if (!banner.isEnable()) {
                return false;
            } else {
                banner.setEnable(false);
                if (banner.isTop()) {
                    banner.setTop(false);
                }
                banner.setLastUpdateTime(banner.getLastUpdateTime());
            }
            this.update(banner);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void setNotTop(int id) {
        Banner banner = this.queryBanner(id);
        banner.setTop(false);
        this.doedit(banner);
    }

    public void doDelete(int id) {
        this.delete(id);
    }
}
